﻿using System;
using System.Collections.Generic;
using System.Text;
using GameServer.ShootingGame;
using System.Drawing;
using GameServer.ShootingGame.RenderableClasses;
using GameServer.ShootingGame.Physics;
using GameServer.ShootingGame.WeaponsClasses;
using GameServer.ShotInTheDark.Weapons;
using GameServer.ShootingGame.MapClasses;
using GameServer.Common.Utils;
using GameServer.Common.Physics;
using GameServer.Common.Extensions;
using GameServer.ShotInTheDark.Maps;

namespace GameServer.ShotInTheDark
{
    public class GamePlay : GamePlayManager
    {

        public GamePlay(ShooterServer server)
            : base(server)
        {

        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            //OrdinanceCollisions();
            PlayerCollisions();
        }

        protected override void InitializeGame()
        {
            maps = new SITDMaps(this);
            LoadMap("Sample Map");
            StartGame();
            GenerateWeaponPickups(3, 3);
        }

        protected override void LoadMap(string title)
        {
            activeMap = maps.GetMap(title);
        }

        private void OrdinanceCollisions()
        {
            PointF globalPointF;
            Ordinance ord;
            GameObject target;
            double targetCircleRadius;
            PointF targetCircleCentre;
            int i;
            int j;
            int n;
            int m;
            n = PlayerList.Count;
            for (i = 0; i < n; i++)
            {
                target = PlayerList[i];
                m = OrdinanceList.Count;
                try
                {
                    for (j = 0; j < m; j++)
                    {
                        ord = (OrdinanceList[j]);

                        if (target != ord.owner)
                        {
                            if (ShooterPhysics.DetectObjectToOrdinanceCollision(target, ord))
                            {
                                target.collisionEffects.ResolveCollision();
                                ord.collisionEffects.ResolveCollision();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            //			 n = ordinanceHolder.numChildren;
            //			 for(i = 0; i < n; i++) {
            //			 	trace("start");
            //			 	if(ShooterPhysics.detectAndResolveOrdinanceToTileCollision(ordinanceHolder.getChildAt(i) as GameObject)){
            ////			 		trace("true");				
            //			 	}
            //			 	trace("end");
            //			 }

            i = 0;
            while (i < OrdinanceList.Count)
            {
                if (ShooterPhysics.DetectAndResolve_OrdinanceToTileCollision(OrdinanceList[i]))
                    ;
                i++;
            }
        }

        private void PlayerCollisions()
        {
            PlayerObject player;
            int i;
            int n = PlayerList.Count;
            for (i = 0; i < n; i++)
            {
                player = PlayerList[i];
                if (player.isHitDetectionEnabled)
                {
                    PickupCollisions(player);
                    ShooterPhysics.DetectAndResolve_PlayerToTileCollision(player);
                }
            }
        }

        private void PickupCollisions(PlayerObject player)
        {
            RectangleF playerCollidable = player.CollidablePart;
            PointF playerCollidableCentre = playerCollidable.GetCentre();
            double collidable_Radius = playerCollidable.Width / 2.0;
            PointF spawnPoint;
            PickupObject pickup;
            GameObject itemPickedUp;
            int i;
            int n = PickupList.Count;
            for (i = 0; i < n; i++)
            {
                pickup = PickupList[i];
                if (CollisionDetection.PointCircleIntersection(collidable_Radius, playerCollidableCentre.X, playerCollidableCentre.Y, pickup.xPosition, pickup.yPosition))
                {
                    //pickupHolder.removeChild(pickup);
                    itemPickedUp = pickup.PickedUp();
                    if (itemPickedUp is Weapon)
                    {
                        (itemPickedUp as Weapon).AttachedTo = player;
                        server.Broadcast(ShooterServer.MSG_PICKED_UP_WEAPON, player.id, player.userName, pickup.id);
                    }
                    pickup.RemoveObject();
                    GenerateWeaponPickup((itemPickedUp as Weapon).type);
                    break;
                    //					spawnPoint = new Point (Math2.rand(0, playScreen.stage.stageWidth), Math2.rand(0, playScreen.stage.stageHeight));
                    //					pickup.truePosition = spawnPoint;
                }
            }
        }

        override public void PlayerJoined(int userID, string userName)
        {
            Console.WriteLine(userID + " " + userName);
            PlayerObject p = new PlayerObject() { id = userID, userName = userName, GamePlayManager = this };
            SpawnPlayer(p);
        }

        public override void SpawnPlayer(PlayerObject p)
        {
            SetRandomPosition(p);
            base.SpawnPlayer(p);
        }

        public void SetRandomPosition(GameObject g)
        {
            g.xPosition = Math2.RandDouble(random, 100, 900);
            g.yPosition = Math2.RandDouble(random, 100, 500);
        }

        private void GenerateWeaponPickups(int numWeaponTypes, int totalWeaponCount)
        {
            for (int i = 1; i <= numWeaponTypes; i++)
            {
                GenerateWeaponPickup(i);
            }
        }

        public void GenerateWeaponPickup(int weaponType)
        {
            PickupObject weaponPickup = null;

            switch (weaponType)
            {
                case 1:
                    {
                        weaponPickup = BasicGunWeapon.GetPickupObject(this);
                        break;
                    }
                case 2:
                    {
                        weaponPickup = MachineGunWeapon.GetPickupObject(this);
                        break;
                    }
                case 3:
                    {
                        weaponPickup = RocketWeapon.GetPickupObject(this);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (weaponPickup != null)
            {
                weaponPickup.xPosition = Math2.RandDouble(random, 100, 600);
                weaponPickup.yPosition = Math2.RandDouble(random, 150, 450);
                weaponPickup.StartUpdate();
            }

            if (Server.Users.Length > 0)
                Server.Broadcast(ShooterServer.MSG_SPAWN_WEAPON, weaponPickup.id, weaponType, weaponPickup.xPosition, weaponPickup.yPosition);
        }


    }
}
